<template>
  <!--3 ytq退货申请处理 -->
  <div class="breadcrumb">
    <Breadcrumb :aaa="aaa" :msgg="msg" :txt="txt" :path1="path1" />
  </div>

  <div class="search">
    <!-- 卡片1 -->
    <div class="search_container">
      <el-card>
        <div class="search_top">
          <span> <i></i> <span> 筛选搜索</span> </span>
          <span>
            <el-button @click="onClear">重置</el-button>
            <el-button type="primary" @click="onSearch">查询搜索</el-button>
          </span>
        </div>
        <el-form label-width="140px" class="f">
          <!-- 1 -->
          <el-form-item label="输入搜索：">
            <el-input type="text" placeholder="服务单号" v-model="id"></el-input>
          </el-form-item>
          <!-- 2 -->
          <el-form-item label="处理状态：">
            <el-select v-model="select" placeholder="全部">
              <el-option label="待处理" value="1" />
              <el-option label="退货中" value="2" />
              <el-option label="已完成" value="3" />
              <el-option label="已拒绝" value="4" />
            </el-select>
          </el-form-item>
          <!-- 3 -->
          <el-form-item label="申请时间：" class="time1">
            <el-date-picker v-model="calIpt1" type="date" placeholder="请选择时间" />
          </el-form-item>
          <!-- 4 -->
          <el-form-item label="操作人员：">
            <el-input type="text" placeholder="全部" v-model="hdMan"></el-input>
          </el-form-item>
          <!-- 5 -->
          <el-form-item label="处理时间：" class="time2">
            <el-date-picker v-model="calIpt2" type="date" placeholder="请选择时间" />
          </el-form-item>
        </el-form>
      </el-card>
    </div>

    <!-- 卡片2 -->
    <el-card class="btn_out" style="margin-top: 20px;">
      <div class="btn">
        <div>数据列表</div>
      </div>
    </el-card>

    <!-- 数据的展示 -->
    <el-table row-key="id" :data="tableData" style="width: 100%" @selection-change="handleSelectionChange" :border="true">
      <!-- 多选框 -->
      <el-table-column type="selection" fixed select-on-indeterminate width="60">
      </el-table-column>
      <el-table-column prop="id" label="服务单号" />
      <el-table-column prop="createTime" label="申请时间" width="200">
        <template #default="scope">
          <div>
            {{
              scope.row.createTime
              ? dayjs(scope.row.createTime).format("YYYY-MM-DD HH:mm:ss")
              : ""
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="memberUsername" label="用户账号" />
      <el-table-column prop="productRealPrice" label="退款金额" width="180">
        <template #default="scope">
          ￥{{ scope.row.productRealPrice }}</template>
      </el-table-column>
      <el-table-column prop="" label="申请状态" width="180">
        <template #default="scope">
          <span v-if="scope.row.status === 0">待处理</span>
          <span v-if="scope.row.status === 1">退货中</span>
          <span v-if="scope.row.status === 2">已完成</span>
          <span v-if="scope.row.status === 3">已拒绝</span>
        </template>
        <!-- <span v-for="item in status2" :key="item.value" :label="item.label">{{ item.value }}</span> -->
      </el-table-column>
      <el-table-column prop="handleTime" label="处理时间" width="200">
        <template #default="scope">
          <div>
            {{
              scope.row.handleTime
              ? dayjs(scope.row.handleTime).format("YYYY-MM-DD HH:mm:ss")
              : ""
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="150" flex="right">
        <template #default="scope">
          <el-button @click="onDetail(scope.row.id)">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="bottom f">
      <!-- 批量删除 -->
      <el-form-item class="delete">
        <el-select v-model="delete1" placeholder="全部">
          <el-option label="批量删除" value="1" />
        </el-select>
        <el-button style="margin-left: 15px" type="primary" @click="onDelete">确定</el-button>
      </el-form-item>

      <!-- 分页器 -->
      <el-pagination class="pagination" v-model:current-page="pagination.pageNum" v-model:page-size="pagination.pageSize"
        background :page-sizes="[5, 10, 15]" layout="total, sizes, prev, pager, next, jumper" :total="pagination.total"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>

    <!-- 查看详情组件 -->
    <!-- <Edit :visible="visible" :form="modalForm" @cancel="onCloseDialog" @confirm="onConfirmDialog"></Edit> -->
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
import type { Ref } from "vue";
import * as api from "../../request/api";
import useTable from "../../hooks/useTables";
import dayjs from "dayjs";
import Edit from "../../components/Edit.vue";
import type { IAdminListItem } from "../../request/QmsAdminConreiller/index";
import { ElMessage, ElMessageBox } from "element-plus";
import { ElTable } from "element-plus";
import router from "../../router";
import Breadcrumb from "../../components/breadcrumb.vue";
let msg = ref("退货申请处理");
let txt = ref("订单");
let path1 = ref("oms/order");
let aaa = ref(0);

let add = false; // false编辑 true 新增
let modalForm = ref({}) as Ref<IAdminListItem>;

//输入框绑定值
let id = ref(""); //服务单号
const select = ref(""); //处理状态
const calIpt1 = ref(""); //申请时间
const hdMan = ref(""); //操作人员
const calIpt2 = ref(""); //处理时间
const delete1 = ref("1"); //批量删除
const isChecked = ref(false); //多选框

//查询搜索请求
let {
  tableData,
  getTableData,
  pagination,
  handleSizeChange,
  handleCurrentChange,
} = useTable(api.returnApply.returnApply1, {
  id,
  select,
  calIpt1,
  calIpt2,
  hdMan,
});
let onSearch = async () => {
  // console.log(id.value);
  // getTableData();
  // let listData = await api.returnApply.returnApply1({
  //   id: id.value,
  //   select: select.value,
  //   calIpt1: calIpt1.value,
  //   hdMan: hdMan.value,
  //   calIpt2: calIpt2.value,
  //   pageNum: 1,
  //   pageSize: 10,
  // });
  // tableData = listData;
  getTableData();
};
//重置
let onClear = () => {
  id.value = "";
  calIpt1.value = "";
  calIpt2.value = "";
  hdMan.value = "";
  select.value = "";
};
//多选框
interface User {
  date: string;
  name: string;
  address: string;
}

const multipleSelection = ref<User[]>([]);

//批量删除
// let ids = ref([]);
// const handleSelectionChange = (val: any) => {
//   let a = val.map((item: any) => {
//     return item.id;
//   });
//   ids.value = a;
//   console.log(ids.value);
// };
let ids = ref([]);
const handleSelectionChange = (val: any) => {
  let a = val.map((item: any) => {
    return item.id;
  });
  ids.value = a;
  console.log(ids.value);
};
//确定删除
let onDelete = () => {
  ids.value.forEach(async (item) => {
    await api.returnApply.returnDel(item);
    ElMessage({
      message: "批量删除成功",
      type: "success",
    });
  });

  getTableData();
  // console.log(multipleSelection.value);
  // if (multipleSelection.value.length == 0) {
  //   ElMessage({
  //     message: "请选择要操作的申请",
  //     type: "warning",
  //     duration: 1000,
  //   });
  //   return;
  // }
  // ElMessageBox.confirm("是否要进行删除操作?", "提示", {
  //   confirmButtonText: "确定",
  //   cancelButtonText: "取消",
  //   type: "warning",
  // })
  //   .then(async () => {
  //     ElMessage({
  //       type: "success",
  //       message: "删除成功",
  //     });
  //   })
  //   .catch(() => {
  //     ElMessage({
  //       type: "info",
  //       message: "取消删除",
  //     });
  //   });
  // tableData.splice(multipleSelection, 1);
};
//查看详情
let onDetail = (myid: any) => {
  getTableData();
  router.push({ path: "/oms/returnApplyDetail", query: { id: myid } });
};
//申请状态
// const status2 = [
//   {
//     label: "0",
//     value: "待处理"
//   },
//   {
//     label: "1",
//     value: "退货中"
//   },
//   {
//     label: "2",
//     value: "已完成"
//   },
//   {
//     label: "3",
//     value: "已拒绝"
//   },
// ]

// 弹框
let visible = ref(false);

// 添加用户
let onAddUser = () => {
  let newUser = {
    email: "",
    nickName: "",
    note: "",
    password: "",
    status: 0,
    username: "",
  } as IAdminListItem;
  modalForm.value = newUser;
  add = true;
  visible.value = true;
};
// 编辑
let onEdit = (row: IAdminListItem) => {
  // console.log(row);
  add = false;
  modalForm.value = row;
  // console.log(modalForm);
  visible.value = true;
};

// 子级emit组件传过来的事件
let onCloseDialog = () => {
  visible.value = false;
};

// 确认了输入框的内容之后
// 需要发送请求
let onConfirmDialog = (row: IAdminListItem) => {
  // console.log(row);  // row就是表单数据
  // 二次 确认
  ElMessageBox.confirm("是否确认提交!", "提示", {
    confirmButtonText: "OK",
    cancelButtonText: "Cancel",
    type: "warning",
  })
    .then(async () => {
      // 发请求
      // 去判断 是 编辑 还是添加
      if (add) {
        await api.admin.adminRegister(row); // 新增
      } else {
        await api.admin.adminUpdate(row); // 编辑
      }

      ElMessage({
        type: "success",
        message: "操作成功",
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消提交",
      });
    });
};

getTableData();
</script>

<style scoped lang="scss">
.f {
  display: flex;
  justify-content: start;
  flex-wrap: wrap;
}

.bottom {
  width: 100%;
  justify-content: space-between;
}

.delete {
  margin-top: 30px;
}

.pagination {
  margin-top: 15px;
}

.el-table--fit {
  border-bottom: 1px solid #ebeef5;
}

.search {
  height: 100vh;
}

:deep .el-table thead {
  font-weight: bold !important;
}

:deep .el-scrollbar__view {
  width: 100% !important;
}

:deep .el-table .el-table__cell {
  text-align: center !important;
}

.el-form--inline .el-form-item {
  margin-right: 10px;
}

.el-form-item__label {
  text-align: right;
  vertical-align: middle;
  float: left;
  font-size: 14px;
  color: #606266;
  line-height: 40px;
  padding: 0 12px 0 0;
  box-sizing: border-box;
}

.el-form-item--mini.el-form-item,
.el-form-item--small.el-form-item {
  margin-bottom: 18px;
}

.el-form--inline .el-form-item__label {
  float: none;
  display: inline-block;
}

.el-form-item__label {
  line-height: 32px;
}

.el-card.is-always-shadow {
  box-shadow: none;
  width: 100%;
}

.search_container {
  left: 0;
  right: 0;
  /* padding: 27px 35px 15px 0; */
  width: 100%;
}

.breadcrumb {
  height: 45px;
  border-bottom: 1px solid #ebeef5;
  font-size: 20px;
}

.search {
  padding: 20px 15px 20px 15px;
  background-color: #fff;
  margin-bottom: 20px;

  .search_top {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
  }
}

//table表格
:deep .el-table .el-table__cell {
  text-align: center !important;
}

:deep .el-table thead {
  font-weight: bold !important;
}

.btn {
  background-color: #fff;
  display: flex;
  justify-content: space-between;
}

.btn_out {
  margin-bottom: 20px;
}

el-card {
  :deep .el-input {
    width: 200px !important;
  }
}

:deep .el-button {
  font-size: 12px !important;
}
</style>
